package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.TUser;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.TUserService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2020-09-12 19:23:48
 */
@RestController
@RequestMapping("tUser")
public class TUserController {

    Logger log = LoggerFactory.getLogger(this.getClass());


    /**
     * 服务对象
     */
    @Resource
    private TUserService tUserService;

    private volatile int i = 0;


    @GetMapping("/init")
    public Object init() {
        i++;
        log.info("init:" + i);
        return "init";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public TUser selectOne(Long id) {
        return this.tUserService.queryById(id);
    }


    @PostMapping("/json")
    public Object json(@RequestBody JSONObject jsonObject) {
        String name = jsonObject.getString("name");
        log.info(jsonObject.toJSONString());
        jsonObject.put("time", System.nanoTime());
        jsonObject.put("now",new Date());
        return jsonObject;
    }


    @GetMapping("/a")
    public Object a(){
        TUser user=new TUser();
        user.setAge(23);
        user.setCreateTime(new Date());
        return user;
    }

}